//
//  ViewController.m
//  ShouFaBao
//
//  Created by User on 14/12/5.
//  Copyright (c) 2014年 HeXieSoftWare. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)rightBarButtonItemSelector:(id)sender
{
//	UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//	[sb instantiateViewControllerWithIdentifier:@"SettingViewController"];
	SettingTableViewController *vc = [[SettingTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLoad {
	self.mTitle = @"收发宝";
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemSelector:)];
	addButton.tintColor = nil;
	self.navigationItem.rightBarButtonItem = addButton;

	/*
	//版本更新
	NSString * urlHostString = [NSString stringWithFormat:@"%@/api/App/GetVersion?appId=HMP_005&platform=%@&deviceId=%@",kURLmain, kSys_PlatForm, kSys_deviceid];

	AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
	[manager GET:urlHostString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
		NSLog(@"%@", responseObject);
		NSString *localVersion =[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
		if (![localVersion isEqualToString:[responseObject objectForKey:@"Version"]]) {
			NSString * url = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://mip.helichina.com:%@/setup/ios/HMP_005/%@",UPPORT,[responseObject objectForKey:@"DownloadUrl"]];

			NSURL * urlApp = [NSURL URLWithString:url];
			[[UIApplication sharedApplication] openURL:urlApp];

			return ;
		}

		[self startLogin];

	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"%@", error);
	}];
*/

#pragma mark 去掉 [self startLogin];
	[self startLogin];

	self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
	self.tableView.bounces = NO;
	self.tableView.backgroundColor = self.tableView.separatorColor;
	self.tableView.separatorColor = [UIColor clearColor];
	[self.view addSubview:self.tableView];

	NSString* filePath = [[NSBundle mainBundle] pathForResource:@"welcome1.gif" ofType:nil];
	NSData* imageData = [NSData dataWithContentsOfFile:filePath];

	_gifImageView = [[SCGIFImageView alloc] initWithFrame:self.navigationController.view.frame];
	[_gifImageView setData:imageData];

	[self.navigationController.view addSubview:_gifImageView];
	_gifImageView.hidden = NO;
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	switch (alertView.tag)
	{
		case kAlertViewTag_isRegister:
		{
			switch (buttonIndex)
			{
				case 0:
				{
					exit(0);
				}
					break;
				case 1:
				{
					_gifImageView.hidden = YES;
					RegisterViewController *vc = [[RegisterViewController alloc] initWithNibName:nil bundle:nil];
					UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
					[self.navigationController presentViewController:nc animated:YES completion:nil];
				}
					break;
				default:
				{
				}
					break;

			}
		}
			break;

		default:
		{
		}
			break;

	}
}

- (void)startLogin
{
	gDataStore = [DataStore sharedDataStore];
//	KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Account Number"
//																	   accessGroup:[NSString stringWithFormat:@"%@.%@", @"APPID", [[NSBundle mainBundle] bundleIdentifier]]];
//	[wrapper resetKeychainItem];
//	gwrapper = wrapper;


	NSDictionary *dictionary = @{@"appid"		: kSys_appid_HMP_005,
								 @"deviceid"	: kSys_deviceid,//[OpenUDID value],
								 @"userName"	: @""//account,
								 };
	[[NetWorkRequestApi sharedNetWorkRequestApi] nrLogin:dictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
		NSLog(@"%@", responseObject);
		switch ([[responseObject objectForKey:@"Tag"] integerValue]) {
			case 1:
			{
				[[iToast makeText:[responseObject objectForKey:@"Message"]] show];
				gUserInfo = responseObject;
				gToken = [responseObject objectForKey:@"Token"];


				switch (gDataStore.mStartPageType) {
					case kStartPageType_1:
					{
						_gifImageView.hidden = YES;
//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
//						vc.title = @"零部件报工";
//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_SpareParts;
//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					case kStartPageType_2:
					{

						_gifImageView.hidden = YES;
						//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
						//						vc.title = @"跟单报工";
						//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_Merchandiser;
						//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					case kStartPageType_3:
					{

						_gifImageView.hidden = YES;
						//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
						//						vc.title = @"跟单报工";
						//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_Merchandiser;
						//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					case kStartPageType_4:
					{

						_gifImageView.hidden = YES;
						//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
						//						vc.title = @"跟单报工";
						//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_Merchandiser;
						//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					case kStartPageType_5:
					{

						_gifImageView.hidden = YES;
						//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
						//						vc.title = @"跟单报工";
						//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_Merchandiser;
						//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					case kStartPageType_6:
					{

						_gifImageView.hidden = YES;
						//						BaoGongDanViewController *vc = [[BaoGongDanViewController alloc] initWithNibName:nil bundle:nil];
						//						vc.title = @"跟单报工";
						//						vc.mBaoGongDanViewControllerType = kBaoGongDanViewControllerType_Merchandiser;
						//						[self.navigationController pushViewController:vc animated:YES];
					}
						break;

					default:
						break;
				}
			}
				break;
#pragma mark 去掉注释
				/*
			case 2:
			{
				UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:[responseObject objectForKey:@"Message"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
				alertView.tag = kAlertViewTag_isRegister;
				[alertView show];
			}
				break;
				 */
			default:
				break;
		}
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

		[[iToast makeText:kNRStr_LoadDataFailure] show];
	}];
	
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return CGRectGetHeight(self.view.frame)-320;//194;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 320;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner_home"]];

	return iv;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	switch (indexPath.row) {
  case 0:
		{
			static NSString *strIdentifier = @"TableViewCell";
			TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
			if (!cell) {
				cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] objectAtIndex:0];

				[cell.btn1 setImageAndTitle:[UIImage imageNamed:@"hpbtn1"] title:@"外部采购订单收货"];
				[cell.btn2 setImageAndTitle:[UIImage imageNamed:@"hpbtn2"] title:@"工厂采购订单发货"];
				[cell.btn3 setImageAndTitle:[UIImage imageNamed:@"hpbtn3"] title:@"工厂采购订单收货"];

				[cell.btn4 setImageAndTitle:[UIImage imageNamed:@"hpbtn4"] title:@"预留订单发货"];
				[cell.btn5 setImageAndTitle:[UIImage imageNamed:@"hpbtn5"] title:@"生产订单发料"];
				[cell.btn6 setImageAndTitle:[UIImage imageNamed:@"hpbtn6"] title:@"物料库存查询"];

				[cell.btn7 setImageAndTitle:[UIImage imageNamed:@"hpbtn7"] title:@"新闻动态"];
				[cell.btn8 setImageAndTitle:[UIImage imageNamed:@"hpbtn8"] title:@"员工天地"];
				[cell.btn9 setImageAndTitle:[UIImage imageNamed:@"hpbtn9"] title:@"添加更多"];

			}
			cell.selectionStyle = UITableViewCellSelectionStyleNone;


			cell.btn1.tag = 1;
			cell.btn2.tag = 2;
			cell.btn3.tag = 3;

			cell.btn4.tag = 4;
			cell.btn5.tag = 5;
			cell.btn6.tag = 6;

			cell.btn7.tag = 7;
			cell.btn8.tag = 8;
			cell.btn9.tag = 9;

			[cell.btn1 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn2 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn3 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];

			[cell.btn4 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn5 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn6 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];

			[cell.btn7 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn8 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
			[cell.btn9 addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];

			return cell;
		}
			break;

  default:
			return nil;
			break;
	}
}

- (void)btnSelector:(HPButton *)sender
{
	switch (sender.tag) {
  case 1:
		{
			ExternPurchaseOrderDeliveryViewController *vc = [[ExternPurchaseOrderDeliveryViewController alloc] initWithNibName:nil bundle:nil];
			vc.mTitle = sender.label.text;
			[self.navigationController pushViewController:vc animated:YES];
		}
			break;

  case 2:
		{

		}
			break;

  case 3:
		{

		}
			break;

  case 4:
		{

		}
			break;

  case 5:
		{

		}
			break;

  case 6:
		{

		}
			break;

  case 7:
		{

		}
			break;

  case 8:
		{

		}
			break;

  case 9:
		{

		}
			break;

  default:
			break;
	}
}


@end
